<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="hAssign(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(scope.row)">编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="hDel(scope.row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :total="total"
                :page-size="pageParams.pagesize"
                @current-change="hCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit? '编辑弹层':'新增弹层'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="hClose"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>

      <el-dialog
        title="分配权限"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialogPermission"
      >
        <assign-permission v-if="showDialogPermission" :row-id="rowId" @close="close" />
      </el-dialog></div>
  </div>
</template>
<script>
import assignPermission from './assignPermission.vue'
import { getRoles, deleteRole, addRole, updateRole } from '@/api/settings'
export default {
  components: { assignPermission },
  data() {
    return {
      roles: [],
      pageParams: {
        page: 1, // 查询第一页
        pagesize: 5 // 每页两条  --- 要与pagination中page-size一致
      },
      total: 0, // 总的数据条数
      showDialog: false, // 控制表单显示隐藏
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      isEdit: false,
      showDialogPermission: false, // 分配权限
      rowId: ''// 角色id
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    hClose() {
      // console.log('close...............')
      // 重置表单
      // this.roleForm = { // 表单数据项
      //   name: '',
      //   description: ''
      // }
      this.$refs.roleForm.resetFields()
    },
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams)
        // console.log(res)
        this.roles = res.data.rows
        this.total = res.data.total
      } catch (err) {
        console.log(err)
      }
    },
    // 用户点击了页码
    hCurrentChange(curPage) {
      // alert(curPage)
      // 1. 更新页码
      this.pageParams.page = curPage
      // 2. 重发请求
      this.loadRoles()
    },
    async doDel(id) {
      try {
        const res = await deleteRole(id)
        // console.log(res)
        this.loadRoles()
        this.$message.success(res.message)
        if (this.roles.length === 1) {
          this.pageParams.page--
        } else {
          this.pageParams.page = 1
        }
        // 重新渲染页面
      } catch (err) {
        console.log(err)
      }
    },
    hDel(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(err => err)
    },
    hAdd() {
      this.showDialog = true
      this.isEdit = false
    },
    // 实现增加操作
    async doAdd() {
      try {
        const res = await addRole(this.roleForm)
        // console.log(res)
        this.$message.success('新增成功')
        // 进入到最后一页
        this.total++
        this.pageParams.page = Math.ceil((this.total) / this.pageParams.pagesize)
        this.loadRoles()
        this.showDialog = false
        this.$refs.roleForm.resetFields()
        this.isEdit = false
      } catch (err) {
        this.$message.error('新增失败')
        console.log(err)
      }
    },
    // 用户点击弹框确定
    hSubmit() {
      // 表单兜底校验
      this.$refs.roleForm.validate(valid => {
        if (!valid) return
        this.isEdit ? this.doEdit() : this.doAdd()
      })
    },
    // 点击编辑
    hEdit(data) {
      this.roleForm = { ...data }
      this.showDialog = true
      this.isEdit = true
    },
    // 编辑具体操作
    async doEdit() {
      try {
        await updateRole(this.roleForm)
        // console.log(res)
        this.$message.success('编辑成功')
        this.loadRoles()
        this.showDialog = false
      } catch (err) {
        this.$message.error('编辑失败')
        console.log(err)
      }
    },
    // 用户点击分配权限按钮
    hAssign(id) {
      this.showDialogPermission = true
      this.rowId = id
    },
    // 关闭角色分配弹窗
    close() {
      this.showDialogPermission = false
      // this.$message.success('分配角色成功')
    }
  }
}
</script>
